/**
 * @Title: RuleController.java
 * @Package com.lemon.controller.cityRun
 * @Description: (一句话阐述):
 * @author Ran_cc
 * @date 2018年10月16日
 * @version V1.0
 */
package com.lemon.controller.cityRun;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.lemon.conf.MyException;
import com.lemon.conf.MyInterceptor.LoginRequired;
import com.lemon.model.cityRun.CrShopUser;
import com.lemon.model.cityRun.CrShops;
import com.lemon.service.cityRun.CrShopUserService;
import com.lemon.service.cityRun.CrShopsService;
import com.lemon.utils.MD5;
import com.lemon.utils.ReqResult;
import com.lemon.utils.StringUtil;
import com.lemon.utils.TokenUtils;

/**
 * <p>
 * Title: RuleController
 * </p>
 * <p>
 * Description:
 * </p>
 * <p>
 * Company:
 * </p>
 * 
 * @author Ran_cc
 * @date 2018年10月16日
 * @版本 V 1.0
 */
@RestController
@RequestMapping("/cru")
public class LoginController {

	@Resource
	private CrShopUserService userService;
	@Resource
	private CrShopsService shopService;

	/**
	 * <b>Description:<blockquote> 登陆接口
	 * </blockquote></b>
	 * 
	 * @Title: login
	 * @param au      用户名
	 * @param pd      密码
	 * @param request
	 * @return
	 * @author Ran_cc
	 * @date 2018年6月17日
	 */
	@RequestMapping(value = "/login.do" , method = RequestMethod.POST)
	public ReqResult login(String au , String pd , HttpServletRequest request , HttpServletResponse response) {
		ReqResult reqResult = new ReqResult();
		try {
			CrShopUser user = new CrShopUser();
			user.setUserAccount(au);
			user.setOrderBy(" order by create_time desc ");
			CrShopUser u = userService.selectBySelective(user);
			if (null != u) {
				//判断密码是否相等
				String pwd = u.getUserPwd();
				if (pd.toUpperCase().equals(pwd.toUpperCase())) {
					//判断账号状态
					if (1 == u.getUserStatus()) {
						CrShops cs = new CrShops();
						cs.setShopUserId(u.getId());
						//						List<CrShops> list = shopService.selectListBySelective(cs);
						CrShops selectShopAndUserBySel = shopService.selectShopAndUserBySel(cs);
						//登陆成功后将店铺信息返回
						reqResult.setReturnResult(selectShopAndUserBySel , ReqResult.code_Success);
						//生成token令牌
						String token = TokenUtils.createJwtToken(u.getUserAccount());
						response.setHeader("accessToken" , token);
					} else {
						reqResult.setReturnResult("账号异常" , ReqResult.code_no_permission);
					}
				} else {
					reqResult.setReturnResult("账号密码错误" , ReqResult.code_login_error);
				}
			} else {
				reqResult.setReturnResult("账号不存在" , ReqResult.code_not_exist);
			}
		} catch (Exception e) {
			// 记录系统错误的登陆记录
			reqResult = new ReqResult("err", ReqResult.code_error);
			MyException.writeErr(e , "操作失败,请稍后重试");
		}
		return reqResult;
	}

	@RequestMapping(value = "/bindCid" , method = RequestMethod.POST)
	public ReqResult bindCid(String cid , String id , HttpServletRequest request , HttpServletResponse response) {
		ReqResult reqResult = new ReqResult();
		try {
			CrShopUser user = new CrShopUser();
			user.setCid(cid);
			//先把其他的绑定的设备删除掉,绑定到自己的账号上面来
			CrShopUser user2 = userService.selectBySelective(user);
			if (user2 != null) {
				user2.setCid("");
				userService.updateUserInfo(user2);
			}
			user.setId(id);
			userService.updateUserInfo(user);
			reqResult.setReturnResult("1");
		} catch (Exception e) {
			reqResult = new ReqResult("", ReqResult.code_error);
			MyException.writeErr(e , "操作失败,请稍后重试");
		}
		return reqResult;
	}

	//退出登录取消当前账号的cid绑定信息
	@RequestMapping(value = "/celBindCid" , method = RequestMethod.POST)
	public ReqResult celBindCid(String cid , String id , HttpServletRequest request , HttpServletResponse response) {
		ReqResult reqResult = new ReqResult();
		try {
			CrShopUser user = new CrShopUser();
			CrShopUser cidUser = userService.selectById(id);
			if (StringUtil.isNotBlank(cidUser.getCid()) && cid.equals(cidUser.getCid())) {
				user.setCid("");
				user.setId(id);
				userService.updateUserInfo(user);
			}
			reqResult.setReturnResult("1");
		} catch (Exception e) {
			reqResult = new ReqResult("", ReqResult.code_error);
			MyException.writeErr(e , "操作失败,请稍后重试");
		}
		return reqResult;
	}

	@LoginRequired
	@RequestMapping(value = "/updatePwd.do" , method = RequestMethod.POST)
	public ReqResult updatePwd(@ModelAttribute CrShopUser user , HttpServletRequest request ,
			HttpServletResponse response) {
		ReqResult reqResult = new ReqResult();
		try {
			String uid = request.getParameter("uid");
			String pwd = request.getParameter("pwd");
			String npwd = request.getParameter("npwd");
			//查询出来用户的原来的密码 
			CrShopUser user2 = userService.selectById(uid);
			//校验原密码是否相等
			if (user2.getUserPwd().equals(pwd.toUpperCase())) {
				//修改新密码 
				user.setUserPwd(npwd.toUpperCase());
				user.setId(uid);
				int row = userService.updateAdminUserInfo(user);
				reqResult.setReturnResult(row);
			} else {
				reqResult.setReturnResult("" , ReqResult.code_login_error);
				return reqResult;
			}
		} catch (Exception e) {
			reqResult.setReturnResult("" , ReqResult.code_error);
			MyException.writeErr(e , "查询客户或管理员列表失败");
		}
		return reqResult;
	}

	public static void main(String[] args) {
		String md5 = MD5.getMD5("123456");
		System.out.println(md5);
	}
}
